package com.cqs.leetcode.dp;

/**
 * @author lixiaowen
 * @create 2019-12-14
 */
public class UniquePathsII63 {

    /**
     *
     * @param og
     * @return
     */
    public int uniquePathsWithObstacles(int[][] og) {
        if(og == null || og.length == 0 || og[0].length == 0) return 0;
        //重要 特殊的情况要考虑到
        if(og[0][0] == 1) return 0;
        int row = og.length;
        int cell = og[0].length;
        int[][] result = new int[row][cell];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < cell; j++) {
                if (i == 0 && j == 0){
                    result[0][0] = 1 - og[0][0];
                }else if (i == 0 && j == 1){
                    result[0][1] = 1 - (og[0][1] | og[0][0]);
                }else if (i == 1 && j == 0){
                    result[1][0] = 1 - (og[1][0] | og[0][0]);
                } else {
                    //防止越界
                    int val =  i == 0 ? 0 : result[i-1][j];
                    int val2 = j == 0 ? 0 : result[i][j-1];
                    result[i][j] = og[i][j] == 1 ? 0 : val + val2;
                }
            }
        }
        return result[row-1][cell-1];
    }
}
